package dao.daoimpl;

import dao.UserDao;
import util.DataBase;
import bean.User;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

/*UserDao的实现类*/
public class UserDaoImpl implements UserDao {
    /*用户登录操作*/
    @Override
    public User login(String account, String password) throws SQLException {
        User user=null;
        String sql="select * from user_table where user_account='"+account+"'and user_password='"+password+"'";
        DataBase.init();
        ResultSet resultSet= DataBase.inquire(sql);
        while(resultSet.next()){
            if(account.equals(resultSet.getString("user_account"))&&password.equals(resultSet.getString("user_password"))){
                user=new User(account,password);
                user.setAddress(resultSet.getString("user_address"));
                user.setBirthday(resultSet.getString("user_birthday"));
                user.setName(resultSet.getString("user_name"));
                user.setScreenName(resultSet.getString("user_screenName"));
                user.setEmail(resultSet.getString("user_email"));
                user.setHeadPortrait(resultSet.getString("user_headPortrait"));
                user.setSign(resultSet.getString("user_sign"));
                user.setSex(resultSet.getString("user_sex"));
                user.setPhone(resultSet.getString("user_phone"));
                user.setAge(resultSet.getInt("user_age"));

                user.setIsAdmin(resultSet.getInt("user_isAdmin"));
            }
        }
        DataBase.release();
        return user;
    }

    @Override
    public boolean register(User user) {
        String account=user.getAccount();
        String password=user.getPsw();
        String qqEmail= user.getEmail();
        int isAdmin=user.getIsAdmin();
        String sql= "insert into user_table(user_account,user_password,user_email,user_isAdmin) values ('"+ account+"','"+password+"','"+qqEmail+"','"+isAdmin+"')";
        DataBase.init();
        int flag= DataBase.operate(sql);
        DataBase.release();
        if(flag>0){
            return true;
        }
        return false;
    }

    @Override
    public boolean forget(String account, String password) {
        String sql = "update user_table set user_password= '" + password + "' where user_account = '" + account+ "'";
        DataBase.init();
        int flag= DataBase.operate(sql);
        DataBase.release();
        if(flag>0){
            return true;
        }
        return false;
    }

    @Override
    public boolean update(User user) {
        String sql="update user_table set user_screenName= '"+user.getScreenName()+"'" +
                ",user_sex='"+user.getSex()+"',user_sign='"+user.getSign()+"',user_age='"+user.getAge()+"',user_birthday='"+user.getBirthday()+"'," +
                "user_phone='"+user.getPhone()+"',user_address='"+user.getAddress()+"',user_name='"+user.getName()+"'" +
                ",user_headPortrait='"+user.getHeadPortrait()+"'where user_account='"+user.getAccount()+"' and user_email='"+user.getEmail()+"'";
        DataBase.init();
        int flag= DataBase.operate(sql);
        System.out.println(flag);
        DataBase.release();
        if(flag>0){
            return true;
        }
        return false;
    }

    @Override
    public ArrayList<User> getAllUsers() throws SQLException {
        ArrayList<User> users=new ArrayList<>();
        String sql="select * from user_table";
        DataBase.init();
        ResultSet resultSet= DataBase.inquire(sql);
        while (resultSet.next()){
            User user=new User();
            user.setAccount(resultSet.getString("user_account"));
            user.setPsw(resultSet.getString("user_password"));
            user.setAddress(resultSet.getString("user_address"));
            user.setBirthday(resultSet.getString("user_birthday"));
            user.setName(resultSet.getString("user_name"));
            user.setScreenName(resultSet.getString("user_screenName"));
            user.setEmail(resultSet.getString("user_email"));
            user.setHeadPortrait(resultSet.getString("user_headPortrait"));
            user.setSign(resultSet.getString("user_sign"));
            user.setSex(resultSet.getString("user_sex"));
            user.setPhone(resultSet.getString("user_phone"));
            user.setAge(resultSet.getInt("user_age"));
            user.setIsAdmin(resultSet.getInt("user_isAdmin"));
            users.add(user);
        }
        DataBase.release();
        return users;
    }

    @Override
    public User getUser(String account) throws SQLException {
        User user=null;
        String sql="select * from user_table where user_account='"+account+"'";
        DataBase.init();
        ResultSet resultSet= DataBase.inquire(sql);
        while(resultSet.next()){
                user=new User();
                user.setAccount(resultSet.getString("user_account"));
                user.setPsw(resultSet.getString("user_password"));
                user.setAddress(resultSet.getString("user_address"));
                user.setBirthday(resultSet.getString("user_birthday"));
                user.setName(resultSet.getString("user_name"));
                user.setScreenName(resultSet.getString("user_screenName"));
                user.setEmail(resultSet.getString("user_email"));
                user.setHeadPortrait(resultSet.getString("user_headPortrait"));
                user.setSign(resultSet.getString("user_sign"));
                user.setSex(resultSet.getString("user_sex"));
                user.setPhone(resultSet.getString("user_phone"));
                user.setAge(resultSet.getInt("user_age"));
                user.setIsAdmin(resultSet.getInt("user_isAdmin"));
            }
        DataBase.release();
        return user;
    }
}
